package excel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Iterator;

/**
 * Created by dongjigong on 2018/5/14.
 *
 * @Email osbornjonny@126.com
 * @Description
 */
public class ReadExcel {

    public static void main(String[] args) {
        InputStream inputStream = null;
        try {

            //inputStream = new FileInputStream("../电子教案模板.xlsx");

            inputStream = ClassLoader.class.getResourceAsStream("/电子教案模板.xlsx");
            ;
            Workbook workbook = new XSSFWorkbook(inputStream);
            //int sheets = workbook.getActiveSheetIndex();
            for (Sheet sheet : workbook) {
//                int row  = sheet.getPhysicalNumberOfRows();
                for (Row row : sheet) {
                    //Row sheetRow = sheet.getRow(j);
                    // Iterator<Cell> cellIterator = sheetRow.cellIterator();
//                    while(cellIterator.hasNext()){
//                        Cell cell = cellIterator.next();
//                        CellStyle cellStyle = cell.getCellStyle();
//                        cell.get
//                    }
                    for (Cell cell : row) {
                        CellStyle cellStyle = cell.getCellStyle();
                        CellType cellTypeEnum = cell.getCellTypeEnum();
                        int ordinal = cellTypeEnum.ordinal();
                        switch (cellTypeEnum) {
                            case STRING:
                                System.out.println(cell.getRichStringCellValue().getString());
                                break;
                            case NUMERIC:
                                if (DateUtil.isCellDateFormatted(cell)) {
                                    System.out.println(cell.getDateCellValue());
                                } else {
                                    System.out.println(cell.getNumericCellValue());
                                }
                                break;
                            case BOOLEAN:
                                System.out.println(cell.getBooleanCellValue());
                                break;
                            case FORMULA:
                                System.out.println(cell.getCellFormula());
                                break;
                            case BLANK:
                                System.out.println();
                                break;
                            default:
                                System.out.println();
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
